package com.springboot.test.common.utils;


import com.springboot.test.common.enums.BizConstant;
import com.springboot.test.common.enums.DateFormatConstant;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.joda.time.*;

import java.sql.Time;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.*;
@Slf4j
public class DateTimeUtil {

    public static DateTime getDateTime000000(DateTime dateTime) {
        return dateTime.millisOfDay().withMinimumValue();
    }

    public static DateTime getDateTime235959(DateTime dateTime) {
        return dateTime.millisOfDay().withMaximumValue();
    }

    public static final String YYYY_MM = "yyyy-MM";

    public static final String HH_mm = "HH:mm";

    public static final String YYYY_MM_DD = "yyyy-MM-dd";
    public static final String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
    public static final String YYYY = "yyyy";

    public static final String DAY_START = " 00:00:00";
    public static final String DAY_END = " 23:59:59";


    /**
     * 获取两个时间中的最大值,并返回最大时间
     */
    public static DateTime getMaxDateTime(DateTime dateTime1, DateTime dateTime2) {
        if (dateTime1.isAfter(dateTime2)) {
            return dateTime1;
        }
        return dateTime2;
    }

    /**
     * 获取两个时间中的最大值,并返回最大时间
     */
    public static Date getMaxDateTime(Date date1, Date date2) {
        DateTime dateTime1 = new DateTime(date1);
        DateTime dateTime2 = new DateTime(date2);
        return getMaxDateTime(dateTime1, dateTime2).toDate();
    }

    public static Integer getDistance(Date startDate, Date endDate, DateTimeUnitEnum dateTimeUnitEnum) {
        DateTime startDateTime = new DateTime(startDate);
        DateTime endDateTime = new DateTime(endDate);
        return getDistance(startDateTime, endDateTime, dateTimeUnitEnum);
    }

    /**
     * 计算两个时间之间的时间差
     *
     * @param startDateTime
     * @param endDateTime
     * @param dateTimeUnitEnum
     * @return
     */
    public static Integer getDistance(DateTime startDateTime, DateTime endDateTime, DateTimeUnitEnum dateTimeUnitEnum) {
        if (null == dateTimeUnitEnum) return null;

        switch (dateTimeUnitEnum) {
            case Seconds:
                return Seconds.secondsBetween(startDateTime, endDateTime).getSeconds();
            case Minutes:
                return Minutes.minutesBetween(startDateTime, endDateTime).getMinutes();
            case Hours:
                return Hours.hoursBetween(startDateTime, endDateTime).getHours();
            case Days:
                return Days.daysBetween(startDateTime, endDateTime).getDays();
            case Millisecond:
                return ((Long) (endDateTime.getMillis() - startDateTime.getMillis())).intValue();

        }
        return null;
    }

    public static Integer getDistance(DateTime startDateTime, DateTime endDateTime) {
        return getDistance(startDateTime, endDateTime, DateTimeUnitEnum.Seconds);
    }

    public static Integer getDistance(Date startTime, Date endTime) {
        return getDistance(startTime, endTime, DateTimeUnitEnum.Days);
    }

    public static Integer getDistance(DateTime startDateTime) {
        return getDistance(startDateTime, DateTime.now());
    }

    public static Integer getDistanceMilli(DateTime startDateTime) {
        return getDistance(startDateTime, DateTime.now(), DateTimeUnitEnum.Millisecond);
    }

    public static Date addDays(Date startTime, int days) {
        if (startTime == null) {
            startTime = new Date();
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(startTime);
        calendar.add(Calendar.DATE, days);
        return calendar.getTime();
    }

    public static Date addMonths(Date startTime, int months) {
        if (startTime == null) {
            startTime = new Date();
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(startTime);
        calendar.add(Calendar.MONTH, months);
        return calendar.getTime();
    }

    public static Date addMin(Date startTime, int min) {
        if (startTime == null) {
            startTime = new Date();
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(startTime);
        calendar.add(Calendar.MINUTE, min);
        return calendar.getTime();
    }

    public static Date addSecond(Date startTime, int second) {
        if (startTime == null) {
            startTime = new Date();
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(startTime);
        calendar.add(Calendar.SECOND, second);
        return calendar.getTime();
    }


    public static Date addHour(Date startTime, int hours) {
        if (startTime == null) {
            startTime = new Date();
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(startTime);
        calendar.add(Calendar.HOUR, hours);
        return calendar.getTime();
    }

    public static Date addYears(Date startTime, int years) {
        if (startTime == null) {
            startTime = new Date();
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(startTime);
        calendar.add(Calendar.YEAR, years);
        return calendar.getTime();
    }

    public static String Date2String(Date date) {
        SimpleDateFormat formatter = new SimpleDateFormat(DateFormatConstant.FORMAT_yyyyMMdd_HHmmss);
        String dateString = formatter.format(date);
        return dateString;
    }

    public static String Date2String(Date date, String dateFormatConstant) {
        SimpleDateFormat formatter = new SimpleDateFormat(dateFormatConstant);
        String dateString = formatter.format(date);
        return dateString;
    }

    public static Date String2Date(String date, String dateFormatConstant) {
        try {
            SimpleDateFormat formatter = new SimpleDateFormat(dateFormatConstant);
            Date parse = formatter.parse(date);
            return parse;
        } catch (Exception e) {
            log.info("String2Date系统异常", e);
        }
        return null;

    }

    public static Date String2Date(String date) {
        try {
            SimpleDateFormat formatter = new SimpleDateFormat(DateFormatConstant.FORMAT_yyyyMMdd_HHmmss);
            Date parse = formatter.parse(date);
            return parse;
        } catch (Exception e) {
            log.info("String2Date系统异常", e);
        }

        return null;
    }

    public static String Time2String(Time time) {
        SimpleDateFormat formatter = new SimpleDateFormat("HH:mm");
        String dateString = formatter.format(time);
        return dateString;
    }

    public static Time String2Time(String date) throws Exception {
        SimpleDateFormat format = new SimpleDateFormat("HH:mm");
        Date d = format.parse(date);
        Time time = new Time(d.getTime());
        return time;
    }

    public static String DateConvertString(Date date) {
        SimpleDateFormat formatter = new SimpleDateFormat(DateFormatConstant.FORMAT_yyyyMMdd);
        String dateString = formatter.format(date);
        return dateString;
    }

    public static String DateConvertString2(Date date) {
        SimpleDateFormat formatter = new SimpleDateFormat(DateFormatConstant.FORMAT_yyyyMMdd);
        String dateString = formatter.format(date);
        return dateString;
    }

    public static Date todayLastDate() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        calendar.set(Calendar.MILLISECOND, 999);
        return calendar.getTime();
    }

    //判断是否在规定的时间内签到 nowTime 当前时间 beginTime规定开始时间 endTime规定结束时间
    public static boolean timeCalendar(Date nowTime, Date beginTime, Date endTime) {
        //设置当前时间
        Calendar date = Calendar.getInstance();
        date.setTime(nowTime);
        //设置开始时间
        Calendar amBegin = Calendar.getInstance();
        amBegin.setTime(beginTime);//上午开始时间
        //设置结束时间
        Calendar amEnd = Calendar.getInstance();
        amEnd.setTime(endTime);//上午结束时间
        //处于开始时间之后，和结束时间之前的判断
        if ((date.after(amBegin) && date.before(amEnd))) {
            return true;
        } else {
            return false;
        }
    }

    public static String getDayOfWeek() {
        //获取今日是周几
        Calendar calendar = Calendar.getInstance();
        int day = calendar.get(Calendar.DAY_OF_WEEK) - 1;
        return day + "";
    }

    public static String longTimeToDay(Long ms) {
        Integer ss = 1000;
        Integer mi = ss * 60;
        Integer hh = mi * 60;
        Integer dd = hh * 24;

        Long day = ms / dd;
        Long hour = (ms - day * dd) / hh;
        Long minute = (ms - day * dd - hour * hh) / mi;
        Long second = (ms - day * dd - hour * hh - minute * mi) / ss;

        StringBuffer sb = new StringBuffer();
        if (day > 0) {
            sb.append(day).append("天");
        }
        if (hour > 0) {
            if (hour < 10) {
                sb.append("0").append(hour);
            } else {
                sb.append(hour);
            }

        } else {
            sb.append("00");
        }
        if (minute > 0) {
            if (minute < 10) {
                sb.append(":").append("0").append(minute);
            } else {
                sb.append(":").append(minute);
            }

        } else {
            sb.append(":00");
        }
        if (second > 0) {
            if (second < 10) {
                sb.append(":").append("0").append(second);
            } else {
                sb.append(":").append(second);
            }

        } else {
            sb.append(":00");
        }
        return sb.toString();
    }

    public static String getDateOfWeek(Date date) {

        String[] weekDays = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};

        Calendar cal = Calendar.getInstance();

        cal.setTime(date);

        int w = cal.get(Calendar.DAY_OF_WEEK) - 1;

        if (w < 0) {
            w = 0;
        }

        return weekDays[w];
    }

    public static List<String> getAllDayHourStrList() {

        String[] allHour = {"00", "01", "02", "03", "04", "05", "06", "07", "08", "09",
                "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23"};

        return Arrays.asList(allHour);

    }

    public static List<String> getDays(Date start, Date end) {
        List<String> result = new ArrayList<>();
        Calendar tempStart = Calendar.getInstance();
        tempStart.setTime(start);
//        tempStart.add(Calendar.DAY_OF_YEAR, 1);

        Calendar tempEnd = Calendar.getInstance();
        tempEnd.setTime(end);
        while (tempStart.before(tempEnd)) {
            result.add(dateToStr(tempStart.getTime()));
            tempStart.add(Calendar.DAY_OF_YEAR, 1);
        }
        return result;
    }

    public enum DateTimeUnitEnum {
        Millisecond,
        Seconds,
        Minutes,
        Hours,
        Days
    }

    // 获得当天0点时间
    public static Date getTimesMorning() {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTime();
    }

    // 获得当天中午点时间
    public static Date getTimesMiddle() {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.HOUR_OF_DAY, 12);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTime();
    }

    // 获得当天23:59:59时间
    public static Date getTimesNight() {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.HOUR_OF_DAY, 23);
        cal.set(Calendar.SECOND, 59);
        cal.set(Calendar.MINUTE, 59);
        cal.set(Calendar.MILLISECOND, 999);
        return cal.getTime();
    }

    // 获得当天0点时间
    public static Date getYesterdayNight() {
        Calendar cal = Calendar.getInstance();
        cal.setTimeInMillis(getTimesNight().getTime() - 3600 * 24 * 1000);
        return cal.getTime();
    }

    // 获得昨天0点时间
    public static Date getYesterdayMorning() {
        Calendar cal = Calendar.getInstance();
        cal.setTimeInMillis(getTimesMorning().getTime() - 3600 * 24 * 1000);
        return cal.getTime();
    }

    public static Date getEndTime(Integer day) {
        Calendar todayEnd = Calendar.getInstance();
        if (null != day) {
            todayEnd.add(Calendar.DATE, day);
        }
        todayEnd.set(Calendar.HOUR, 23);
        todayEnd.set(Calendar.MINUTE, 59);
        todayEnd.set(Calendar.SECOND, 59);
        todayEnd.set(Calendar.MILLISECOND, 999);
        return todayEnd.getTime();
    }

    public static String getYearStartTime(Integer day) {
        Calendar calendar = Calendar.getInstance();// 获取当前日期
        calendar.add(Calendar.YEAR, day);
//        calendar.setTimeZone(TimeZone.getTimeZone(timeZone));
//        calendar.setTimeInMillis(timeStamp);
        calendar.add(Calendar.YEAR, 0);
        calendar.add(Calendar.DATE, 0);
        calendar.add(Calendar.MONTH, 0);
        calendar.set(Calendar.DAY_OF_YEAR, 1);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return DateTimeUtil.Date2String(new Date(calendar.getTimeInMillis()));
    }

    public static String getYearEndTime(Integer day) {
        Calendar calendar = Calendar.getInstance();// 获取当前日期
        calendar.add(Calendar.YEAR, day);
        int year = calendar.get(Calendar.YEAR);
        calendar.clear();
        calendar.set(Calendar.YEAR, year);
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        calendar.set(Calendar.MILLISECOND, 999);
        calendar.roll(Calendar.DAY_OF_YEAR, -1);
        return DateTimeUtil.Date2String(new Date(calendar.getTimeInMillis()));
    }

    public static int getMonthDays(int year, int month) {

        if (month == 2) {

            if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {

                return 29;

            } else {

                return 28;

            }

        } else if (month == 4 || month == 6 || month == 9 || month == 11) {

            return 30;

        } else {

            return 31;

        }
    }

    // 获得本月第一天0点时间
    public static Date getTimesMonthmorning() {
        Calendar cal = Calendar.getInstance();
        cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
        cal.set(Calendar.DAY_OF_MONTH, cal.getActualMinimum(Calendar.DAY_OF_MONTH));
        return cal.getTime();
    }

    // 获取下个月第一天的0点时间
    public static Date getTimesMonthnight() {
        Calendar cal = Calendar.getInstance();
        cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
        cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
        cal.set(Calendar.HOUR_OF_DAY, 24);
        return cal.getTime();
    }

    // 通过 开始时间 和 结束时间，获取两个时间段中的 具体某一天
    public static List<String> getPerDay(Date startTime, Date endTime) {
        //定义一个接受时间的集合
        List<String> dateList = new ArrayList<>();
        dateList.add(DateTimeUtil.Date2String(startTime, DateFormatConstant.FORMAT_yyyyMMdd));
        Calendar calBegin = Calendar.getInstance();

        // 使用给定的 Date 设置此 Calendar 的时间
        calBegin.setTime(startTime);
        Calendar calEnd = Calendar.getInstance();

        // 使用给定的 Date 设置此 Calendar 的时间
        calEnd.setTime(endTime);

        // 测试此日期是否在指定日期之后
        while (endTime.after(calBegin.getTime())) {
            // 根据日历的规则，为给定的日历字段添加或减去指定的时间量
            calBegin.add(Calendar.DAY_OF_MONTH, 1);
            dateList.add(DateTimeUtil.Date2String(calBegin.getTime(), DateFormatConstant.FORMAT_yyyyMMdd));
        }
        return dateList;
    }

    // 获取 当前月份 的 最后一天
    public static String getMaxMonthDate() {
        SimpleDateFormat dft = new SimpleDateFormat("yyyyMMdd");
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        // calendar.add(Calendar.MONTH, -1);
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        return dft.format(calendar.getTime());
    }

    // 获取 上个月 的 最后一天
    public static String getLastMaxMonthDate() {
        SimpleDateFormat dft = new SimpleDateFormat("yyyyMMdd");
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.add(Calendar.MONTH, -1);
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        return dft.format(calendar.getTime());
    }

    // 获取 下个月 的 第一天
    public static String getPerFirstDayOfMonth() {
        SimpleDateFormat dft = new SimpleDateFormat("yyyyMMdd");
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MONTH, 1);
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
        return dft.format(calendar.getTime());
    }

    // 获取任意时间的月的最后一天
    public static String getMaxMonthDate(String repeatDate) {
        SimpleDateFormat dft = new SimpleDateFormat("yyyyMMdd");
        Calendar calendar = Calendar.getInstance();
        try {
            if (StringUtils.isNotBlank(repeatDate) && !"null".equals(repeatDate)) {
                calendar.setTime(dft.parse(repeatDate));
            }
        } catch (ParseException e) {
            log.info("getMaxMonthDate系统异常", e);

        }
        // calendar.add(Calendar.MONTH, -1);
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        return dft.format(calendar.getTime());
    }

    // 获取任意时间的月的第一天
    public static String getMinMonthDate(String repeatDate) {
        SimpleDateFormat dft = new SimpleDateFormat("yyyyMMdd");
        Calendar calendar = Calendar.getInstance();
        try {
            if (StringUtils.isNotBlank(repeatDate) && !"null".equals(repeatDate)) {
                calendar.setTime(dft.parse(repeatDate));
            }
        } catch (ParseException e) {
            log.info("getMinMonthDate系统异常", e);

        }
        // calendar.add(Calendar.MONTH, -1);
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
        return dft.format(calendar.getTime());
    }

    // 将 yyyy-MM-dd 类型转化为 Date数据类型
    public static Date switchTimeStringToDate(String dateString) throws ParseException {
        String stringFormat = strToDateFormat(dateString);
        // 定义转化格式
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");//定义一个formate
        Date date = simpleDateFormat.parse(stringFormat);//将formate型转化成Date数据类型
        return date;
    }

    /**
     * 字符串转日期
     *
     * @param dateString
     * @param format
     * @return
     */
    public static Date strToDate(String dateString, String format) {
        SimpleDateFormat formatter = new SimpleDateFormat(format);
        formatter.setLenient(false);
        Date newDate = null;
        try {
            newDate = formatter.parse(dateString);
        } catch (ParseException e) {

        }
        return newDate;
    }

    // 将短时间格式转化为字符串 yyyy-MM-dd
    public static String dateToStr(Date dateDate) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String dateString = formatter.format(dateDate);
        return dateString;
    }

    public static String dateToStrWithFormat(Date date, String format) {
        if (format == null || date == null) {
            return BizConstant.BLANK;
        }
        SimpleDateFormat formatter = new SimpleDateFormat(format);
        String dateString = formatter.format(date);
        return dateString;
    }

    public static String getStartAndEndDateStr(Date date, Date startTime, Date endTime, String toSeparator) {

        if (StringUtils.isBlank(toSeparator)) {
            toSeparator = BizConstant.TO;
        }

        if (date == null) {
            return BizConstant.BLANK;
        } else {
            String dateStr = DateTimeUtil.dateToStrWithFormat(date, DateFormatConstant.FORMAT_yyyyMMdd);
            if (startTime == null || endTime == null) {
                return dateStr;
            }
            String startStr = DateTimeUtil.dateToStrWithFormat(startTime, DateFormatConstant.FORMAT_HHmm);
            String endStr = DateTimeUtil.dateToStrWithFormat(endTime, DateFormatConstant.FORMAT_HHmm);
            return dateStr + BizConstant.SPACING + startStr + BizConstant.SPACING +
                    toSeparator + BizConstant.SPACING + endStr;
        }
    }

    // 将时间格式 yyyy-MM-dd hh:mm:ss 转为为 yyyy-MM-dd
    public static Date longTimeToSortTime(Date date) throws ParseException {
        return switchTimeStringToDate(dateToStr(date));
    }

    // 将 yyyyMMdd 的字符串转化为日期，格式"yyyy-MM-dd"
    public static String strToDateFormat(String date) throws ParseException {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
        formatter.setLenient(false);
        Date newDate = formatter.parse(date);
        formatter = new SimpleDateFormat("yyyy-MM-dd");
        return formatter.format(newDate);
    }


    public static SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
    public static SimpleDateFormat format1 = new SimpleDateFormat("yyyyMMdd HH:mm:ss");

    // 得到指定日期的一天的的最后时刻23:59:59
    public static Date getFinallyDate(Date date) {
        String temp = format.format(date);
        temp += " 23:59:59";

        try {
            return format1.parse(temp);
        } catch (ParseException e) {
            return null;
        }
    }

    // 得到指定日期的一天的开始时刻00:00:00
    public static Date getStartDate(Date date) {
        String temp = format.format(date);
        temp += " 00:00:00";

        try {
            return format1.parse(temp);
        } catch (Exception e) {
            return null;
        }
    }

    // 得到指定日期的一天的开始时刻00:00
    public static String getStartDateHHmm() {
        LocalDateTime date = LocalDateTime.now();
        LocalDateTime of = LocalDateTime.of(date.toLocalDate(), LocalTime.MIN);
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm");
        return of.format(formatter);
    }

    // 得到指定日期的一天的结束时刻23:59
    public static String getEndDateHHmm() {
        LocalDateTime date = LocalDateTime.now();
        LocalDateTime of = LocalDateTime.of(date.toLocalDate(), LocalTime.MAX);
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm");
        return of.format(formatter);
    }

    /**
     * 获取两个日期之间的月份列表 【包括年：2021-11】
     */
    public static List<String> getMonthSetBetweenStartAndEnd(Date startDate, Date endDate) {

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(startDate);
        // 获取开始年份和开始月份
        int startYear = calendar.get(Calendar.YEAR);
        int startMonth = calendar.get(Calendar.MONTH);
        // 获取结束年份和结束月份
        calendar.setTime(endDate);
        int endYear = calendar.get(Calendar.YEAR);
        int endMonth = calendar.get(Calendar.MONTH);

        List<String> list = new ArrayList<>();
        for (int i = startYear; i <= endYear; i++) {
            String date;
            if (startYear == endYear) {
                for (int j = startMonth; j <= endMonth; j++) {
                    if (j < 9) {
                        date = i + "-0" + (j + 1);
                    } else {
                        date = i + "-" + (j + 1);
                    }
                    list.add(date);
                }

            } else {
                if (i == startYear) {
                    for (int j = startMonth; j < 12; j++) {
                        if (j < 9) {
                            date = i + "-0" + (j + 1);
                        } else {
                            date = i + "-" + (j + 1);
                        }
                        list.add(date);
                    }
                } else if (i == endYear) {
                    for (int j = 0; j <= endMonth; j++) {
                        if (j < 9) {
                            date = i + "-0" + (j + 1);
                        } else {
                            date = i + "-" + (j + 1);
                        }
                        list.add(date);
                    }
                } else {
                    for (int j = 0; j < 12; j++) {
                        if (j < 9) {
                            date = i + "-0" + (j + 1);
                        } else {
                            date = i + "-" + (j + 1);
                        }
                        list.add(date);
                    }
                }

            }

        }

        return list;

    }

    /**
     * 获取两个日期之间的月份列表 【只有月份：1】
     */
    private static Set<Integer> getOnlyMonthSetBetweenStartAndEnd(Date startDate, Date endDate) {

        Calendar calendarStart = Calendar.getInstance();
        calendarStart.setTime(startDate);
        int startYear = calendarStart.get(Calendar.YEAR);
        int startMonth = calendarStart.get(Calendar.MONTH);

        Calendar calendarEnd = Calendar.getInstance();
        calendarEnd.setTime(endDate);
        int endYear = calendarEnd.get(Calendar.YEAR);
        int endMonth = calendarEnd.get(Calendar.MONTH);

        Set<Integer> list = new HashSet<>();
        for (int i = startYear; i <= endYear; i++) {
            int date;
            if (startYear == endYear) {
                for (int j = startMonth; j <= endMonth; j++) {
                    if (j < 9) {
                        date = (j + 1);
                    } else {
                        date = (j + 1);
                    }
                    list.add(date);
                }

            } else {
                if (i == startYear) {
                    for (int j = startMonth; j < 12; j++) {
                        if (j < 9) {
                            date = (j + 1);
                        } else {
                            date = (j + 1);
                        }
                        list.add(date);
                    }
                } else if (i == endYear) {
                    for (int j = 0; j <= endMonth; j++) {
                        if (j < 9) {
                            date = (j + 1);
                        } else {
                            date = (j + 1);
                        }
                        list.add(date);
                    }
                } else {
                    for (int j = 0; j < 12; j++) {
                        if (j < 9) {
                            date = (j + 1);
                        } else {
                            date = (j + 1);
                        }
                        list.add(date);
                    }
                }

            }

        }

        return list;

    }


    /**
     * 获取两个日期之间的年份列表
     */
    public static List<String> getYearSetBetweenStartAndEnd(Date startDate, Date endDate) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(startDate);
        // 获取开始年份
        int startYear = calendar.get(Calendar.YEAR);
        // 获取结束年份
        calendar.setTime(endDate);
        int endYear = calendar.get(Calendar.YEAR);

        List<String> list = new ArrayList<>();

        if (startYear == endYear) {
            list.add(String.valueOf(startYear));
        } else {
            for (int i = startYear; i <= endYear; i++) {
                list.add(String.valueOf(i));
            }
        }

        return list;
    }

    public static String queryByStartEndAge(Integer start, Integer end) {
        //计算年龄
        Date d = new Date();
        Calendar c = Calendar.getInstance();
        c.setTime(d);
        c.add(Calendar.YEAR, 0 - end);
        Date timeStart = c.getTime();
        String startAge = DateTimeUtil.dateToStr(timeStart);

        Calendar s = Calendar.getInstance();
        s.setTime(d);
        s.add(Calendar.YEAR, 0 - start);
        Date timeEnd = s.getTime();
        String endAge = DateTimeUtil.dateToStr(timeEnd);

        return startAge + ":" + endAge;

    }

    public static long days(String start, String end) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDate startDate = LocalDate.parse(start, formatter);
        LocalDate endDate = LocalDate.parse(end, formatter);
        return startDate.until(endDate, ChronoUnit.DAYS);

    }

    /**
     * 获取当前系统时间最近n月的年月（含当月）
     */
    public static List<String> getLatestMonth(Date date, int month) {
        List<String> list = new ArrayList<>();
        Calendar from = getCalendar(date, list);
        for (int i = 1; i < month; i++) {
            from.add(Calendar.MONTH, -1);//11个月前
            String str2 = from.get(Calendar.YEAR) + "-" + fillZero(from.get(Calendar.MONTH) + 1);
            list.add(str2);
        }

        return list;
    }

    private static Calendar getCalendar(Date date, List<String> list) {
        Calendar from = Calendar.getInstance();
        from.setTime(date);
        String str1 = from.get(Calendar.YEAR) + "-" + fillZero(from.get(Calendar.MONTH) + 1);
        list.add(str1);
        return from;
    }

    /**
     * 格式化月份
     */
    public static String fillZero(int i) {
        String month = "";
        if (i < 10) {
            month = "0" + i;
        } else {
            month = String.valueOf(i);
        }
        return month;
    }

    public static LocalDate dateToLocaleDate(Date date) {

        Instant instant = date.toInstant();

        ZoneId zoneId = ZoneId.systemDefault();

        return instant.atZone(zoneId).toLocalDate();

    }

    public static List<String> getTimeList(int timeScale) {
        Date start = dayStartDate();//转换为天的起始date
        Date nextDayDate = nextDay(start);//下一天的date

        List<Date> result = new ArrayList<>();
        while (start.compareTo(nextDayDate) < 0) {
            result.add(start);
            //日期加5分钟
            start = addFiveMin(start, timeScale);
        }
        List<String> list = new ArrayList<>(BizConstant.TWO_INITIAL_CAPACITY);
        SimpleDateFormat sdf = new SimpleDateFormat(HH_mm);
        for (Date d : result) {
            list.add(sdf.format(d));
        }
        return list;
    }

    private static Date addFiveMin(Date start, int offset) {
        Calendar c = Calendar.getInstance();
        c.setTime(start);
        c.add(Calendar.MINUTE, offset);
        return c.getTime();
    }

    private static Date nextDay(Date start) {
        Calendar c = Calendar.getInstance();
        c.setTime(start);
        c.add(Calendar.DATE, 1);
        return c.getTime();
    }

    private static Date dayStartDate() {
        Calendar c = Calendar.getInstance();
        c.set(Calendar.HOUR_OF_DAY, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        c.set(Calendar.MILLISECOND, 0);
        return c.getTime();
    }

    public static LocalDate stringToLocalDate(String date) {
        return LocalDate.parse(date, DateTimeFormatter.ofPattern(YYYY_MM_DD));
    }

    public static void main(String[] args) {
        Date start = DateTimeUtil.strToDate("2022-07-07", DateFormatConstant.FORMAT_yyyyMMdd);
        Date end = DateTimeUtil.strToDate("2022-07-14", DateFormatConstant.FORMAT_yyyyMMdd);

        System.out.println(dateToStrWithFormat(addMonths(new Date(), 1), YYYY_MM_DD_HH_MM_SS));
        System.out.println(getPerDay(start, DateTimeUtil.addDays(end, -1)));
    }
}
